package com.jzz.eletuanbackend.mapper;

import com.jzz.eletuanbackend.entity.DelivererOrder;
import com.jzz.eletuanbackend.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    @Select("SELECT * FROM v_order WHERE id=#{id} LIMIT 1")
    Order getById(@Param("id") Long id);

    @Select("SELECT * FROM v_order WHERE customerId=#{cid} ORDER BY `id` DESC")
    List<Order> getByCustomerId(@Param("cid") Long cid);

    @Select("SELECT * FROM v_order WHERE delivererId=#{did} ORDER BY `id` DESC")
    List<Order> getByDelivererId(@Param("did") Long did);

    @Select("SELECT * FROM v_order WHERE shopId = #{id} ORDER BY `id` DESC")
    List<Order> getByShopId(@Param("id") Long id);

    @Insert("INSERT INTO `order` (customerId, shopId, commodity, submitTime, pay, shipPrice, receiverInfo, packPrice, totalPrice) " +
            "SELECT #{cid}, #{sid}, #{commodity}, #{submitTime}, #{pay}, #{shipPrice}, #{receiverInfo}, #{packPrice}, #{totalPrice} " +
            "FROM DUAL WHERE EXISTS (SELECT 1 FROM v_shop WHERE id = #{sid})")
    Integer newOrder(
            @Param("cid") Long cid,
            @Param("sid") Long shopId,
            @Param("commodity") String commodity,
            @Param("submitTime") Long submitTime,
            @Param("pay") Boolean pay,
            @Param("shipPrice") Double shipPrice,
            @Param("receiverInfo") String receiverInfo,
            @Param("packPrice") Double packPrice,
            @Param("totalPrice") Double totalPrice
    );


    @Update("UPDATE `order` SET pay = 1, payTime = #{payTime} WHERE `id` = #{id} AND `customerId` = #{uid} LIMIT 1")
    Integer pay(@Param("id") Long id, @Param("payTime") Long payTime, @Param("uid") Long uid);

    @Update("UPDATE `order` SET `acceptTime`=#{time} WHERE `id`=#{id} LIMIT 1")
    Integer acceptOrder(
            @Param("id") Long id,
            @Param("time") Long time
    );

    @Update("UPDATE `order` SET `delivererTakeTime`=#{time}, `delivererId`=#{did} WHERE `id`=#{id} AND delivererId IS NULL LIMIT 1")
    Integer takeOrder(
            @Param("id") Long id,
            @Param("time") Long time,
            @Param("did") Long did
    );

    @Update("UPDATE `order` SET `delivererArriveTime`=#{time} WHERE `id`=#{id} LIMIT 1")
    Integer arriveOrder(
            @Param("id") Long id,
            @Param("time") Long time
    );

    @Update("UPDATE `order` SET `deliverTime`=#{time} WHERE `id`=#{id} LIMIT 1")
    Integer deliverOrder(
            @Param("id") Long id,
            @Param("time") Long time
    );

    @Update("UPDATE `order` SET `receiveTime`=#{time} WHERE `id`=#{id} LIMIT 1")
    Integer receiveOrder(
            @Param("id") Long id,
            @Param("time") Long time
    );

    @Update("UPDATE `order` SET `cancelTime`=#{time} WHERE `id`=#{id} LIMIT 1")
    Integer cancelOrder(
            @Param("id") Long id,
            @Param("uid") Long uid,
            @Param("time") Long time
    );

    @Insert("INSERT INTO customerordercomment (orderId, shopComment, delivererComment, shopReply, shopStar, deliverStar)" +
            " VALUES (#{id}, #{sc}, #{dc}, '', #{ss}, #{ds})")
    Integer commentOrder(@Param("id") Long id,
                         @Param("sc") String sc,
                         @Param("dc") String dc,
                         @Param("ss") Integer ss,
                         @Param("ds") Integer ds);

    @Select("SELECT `cancel` FROM `v_order_status` WHERE `id` = #{id} LIMIT 1")
    Integer isOrderCanceled(@Param("id") Long id);

    @Select("SELECT `deliver` FROM `v_order_status` WHERE `id` = #{id} LIMIT 1")
    Integer isOrderDelivered(@Param("id") Long id);

    @Select("SELECT * FROM `v_deliverer_order`")
    List<DelivererOrder> availableOrders();
}
